import {makePos, ziDi} from "../engines/managers/ziManager";
import O1HD from "../engines/gameEngine";
import shouShi_database from "./shouShi_database";

const {ccclass, property} = cc._decorator;

@ccclass
export default class gdiLayer extends cc.Component {

    @property(cc.Graphics)
    gdiShadow: cc.Graphics = null;
    @property(cc.Graphics)
    gdi: cc.Graphics = null;
    @property(cc.Label)
    lbTopic: cc.Label = null;
    @property(cc.Label)
    lbDesc: cc.Label = null;
    @property(cc.Label)
    lbAnswer: cc.Label = null;
    @property({type: cc.Label})
    lbAnswerPy: cc.Label = null;

    onLoad() {
        this.node.on('touchstart', this.onTouchStart.bind(this));
        this.node.on('touchmove', this.onTouchMove.bind(this));
        this.node.on('touchend', this.onTouchEnd.bind(this));
        this.node.on('touchcancel', this.onTouchCancel.bind(this));
    }

    start() {
        this.m_minWidth = this.gdi.lineWidth;
    }

    private m_maxWidth = 50;
    private m_minWidth = null;

    // LIFE-CYCLE CALLBACKS:
    lastPos: cc.Vec2 = null;
    allPos: cc.Vec2[] = [];

    //
    waitSkillId: string = null;

    private m_ziId: string;
    private m_ziInfo: ziDi;
    private m_writeCallBack: (zi: string) => void = null;

    setTopic(ziId: string) {
        this.m_ziId = ziId;
        this.m_ziInfo = O1HD.getManagers().ziMgr.getZiDiInfo(ziId);
        this.gdi.clear();
        this.gdiShadow.clear();
        if (!this.m_ziInfo) {
            console.error('#48未知错误 字不存在', ziId);
            return;
        }
        this.lbTopic.string = this.m_ziInfo.name;
        this.lbTopic.node.opacity = 255;
        this.lbAnswer.string = '';
        this.lbAnswer.node.opacity = 0;
        this.lbAnswerPy.string = '';
        this.gdi.node.opacity = 255;
        this.gdiShadow.node.opacity = 255;
    }

    setAnswer(ziId: string) {
        let answer = O1HD.getManagers().ziMgr.getZiDiInfo(ziId);
        this.lbAnswer.string = `${answer.name}`;
        this.lbAnswerPy.string = `[ ${answer.py} ]`;
    }

    showOver(answerZiId: string, overCallBack: () => void) {
        let answer = O1HD.getManagers().ziMgr.getZiDiInfo(answerZiId);
        this.lbAnswer.string = answer.name;
        this.lbAnswer.node.opacity = 0;
        const showTime: number = 1.5;
        cc.tween(this.gdiShadow.node)
            .to(showTime * 0.5, {opacity: 0})
            .start();
        cc.tween(this.gdi.node)
            .to(showTime * 0.5, {opacity: 0})
            .start();
        cc.tween(this.lbTopic.node)
            .to(showTime, {opacity: 0})
            .start();
        cc.tween(this.lbAnswer.node)
            .to(showTime, {opacity: 255})
            .call(() => {
                this.lbAnswerPy.string = `[ ${answer.py} ]`;
                overCallBack();
            })
            .start();
    }

    clearGdi() {
        this.gdi.clear();
        this.gdiShadow.clear();
    }

    setWriteOverFunction(writeCallBack: (ziId: string) => void) {
        this.m_writeCallBack = writeCallBack;
    }

    onTouchStart(event: cc.Event.EventTouch) {
        console.log('onTouchstart');
        
        this.allPos = [];
        this.gdi.clear();
        this.gdiShadow.clear();
        //
        this.lastPos = event.touch.getLocation();
        this.allPos.push(this.lastPos);
        let {x, y} = this.touchPosToScenePos(this.lastPos);
        // this.gdi.circle(x, y, this.gdi.lineWidth * 0.1);
        // this.gdi.stroke();
        this.gdi.moveTo(x, y);
        // this.gdiShadow.circle(x, y, this.gdiShadow.lineWidth * 0.01);
        // this.gdiShadow.stroke();
        this.gdiShadow.moveTo(x, y);
        this.onTouchStarted();
    }

    onTouchMove(event: cc.Event.EventTouch) {
        this.lastPos = event.touch.getLocation();
        this.allPos.push(this.lastPos);
        let {x, y} = this.touchPosToScenePos(this.lastPos);
        let pos:cc.Vec2 = this.touchPosToScenePos(event.getPreviousLocation());
        // this.gdi.moveTo(x,y);
        this.gdi.moveTo(pos.x,pos.y);
        this.gdi.lineTo(x, y);
        // this.gdi.moveTo(x,y);
        this.gdi.stroke();
        this.gdiShadow.moveTo(pos.x,pos.y);
        this.gdiShadow.lineTo(x, y);
        this.gdiShadow.stroke();
        this.onTouchMoved();
    }

    onTouchEnd(event: cc.Event.EventTouch) {
        console.log("touchEndDDDDD");
        this.onTouchOver();
    }

    onTouchCancel(event: cc.Event.EventTouch) {
        console.log("touchCancel");
        this.onTouchOver();
    }

    private m_touchIngWidth: any = null;

    private onTouchStarted() {
        // if (this.m_touchIngWidth == null) {
        //     this.m_touchIngWidth = () => {
        //         this.gdi.lineWidth = Math.min(this.gdi.lineWidth + 1, this.m_maxWidth);
        //     }
        // }
        // this.schedule(this.m_touchIngWidth, 0.3);
    }

    private onTouchMoved() {
        // this.gdi.lineWidth = Math.max(this.gdi.lineWidth - 1, this.m_minWidth);
    }

    private onTouchOver() {
        // this.unschedule(this.m_touchIngWidth);
        console.log("笔画画完")
        let skillId = shouShi_database.getCalcSkillIdOfMap(this.allPos);
        console.log(skillId,"skillId")
        //笔划信息
        let bf = O1HD.getManagers().ziMgr.getZiBiInfo(skillId);
        console.debug('笔划信息', bf);
        //位置信息
        let posInfo: makePos = this.getPosStartEndInfo();
        //获取合成的字
        let makeZiId: string = O1HD.getManagers().ziMgr.getMakeZi(this.m_ziId, bf.id, posInfo);
        console.debug('合成的字', makeZiId);
        if (makeZiId == null) {
            this.lbDesc.string = '完全不对';
        } else {
            if (makeZiId == 'have') {
                this.lbDesc.string = '笔划对了';
            } else {
                this.lbDesc.string = '你写的是:' + O1HD.getManagers().ziMgr.getZiDiInfo(makeZiId).name;
            }
        }
        this.m_writeCallBack && this.m_writeCallBack(makeZiId);
    }

    private getPosStartEndInfo() {
        let startPos = this.touchPosToScenePos(this.allPos[0]);
        let start = O1HD.getManagers().ziMgr.getMakePosOfVec2(startPos, this.node.getContentSize());
        console.debug('开始信息', startPos, start);
        let endPos = this.touchPosToScenePos(this.allPos[this.allPos.length - 1]);
        let end = O1HD.getManagers().ziMgr.getMakePosOfVec2(endPos, this.node.getContentSize());
        console.debug('结束信息', endPos, end);
        return {start, end};
    }

    private touchPosToScenePos(touchPos: cc.Vec2) {
        return cc.v2(
            touchPos.x - this.node.parent.width / 2 - this.node.x,
            touchPos.y - this.node.parent.height / 2 - this.node.y
        );
    }

    private getStartEndPos() {

    }
}
























